package com.wngbms.guava;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class CyclicBarrierTest {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

        Runnable runnable = () -> {
            try {
                cyclicBarrier.await();
                log.info("start");
                Thread.sleep(3000);
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
            log.info("end");
            countDownLatch.countDown();
        };

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            executorService.submit(runnable);
        }

        countDownLatch.await();
        executorService.shutdown();
    }

}
